﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WG计划管理工具
{
    public partial class frmWGCKManage : Form
    {
        int iWorkMode = 0;
        SqlTools.SqlTools sqt;
        string strUserID;

        #region SQL脚本
        //获取当天最大的计划单号
        string strGetMaxPPNo = "select max(出库单号) 最大单号 from wgcka where 出库单号 like convert(varchar(100),getdate(),112)+'%'";
        //获取当天最大的工单号
        string strGetMaxGDNo = "select max(gd001) 最大单号 from gd_table where len(gd001)=12 and gd001 like convert(varchar(100),getdate(),112)+'%'";
        //获取服务器日期
        string strGetServerDate = "select replace(CONVERT(varchar(12) , getdate(), 111),'/','-')";
        //按条件查询指定计划单单身
        string strSelectWGCKARange = "select * from wgcka where 出库单号 in (select distinct b.出库单号 from wgcka a,wgckb b where a.出库单号=b.出库单号";
        //查询指定计划单单身
        string strSelectWGCKB = "select 出库项次,结余号,型号规格,出库数量,生产批号,备注,工单号 from wgckb where 出库单号='@出库单号' order by 出库项次";
        //获取指定计划项次关联的生产计划项次
        string strGetSCJHXC = "select 生产计划号,生产计划项次,生产计划数量 from wgckht where 出库单号='@出库单号' and 出库项次=@出库项次";
        //删除指定单号的单头
        string strDeleteWGCKA = "delete from wgcka where 出库单号='@出库单号'";
        //删除指定单号的单身
        string strDeleteWGCKB = "delete from wgckb where 出库单号='@出库单号'";
        //删除指定单号的合同绑定
        string strDeleteWGCKHT = "delete from wgckht where 出库单号='@出库单号'";
        //插入单头
        string strInsertWGCKA = "insert into wgcka (出库单号) values ('@出库单号')";
        //插入单身
        string strInsertWGCKB = "insert into wgckb (出库单号,出库项次,结余号,型号规格,出库数量,生产批号,备注) values ('@出库单号',@出库项次,'@结余号','@型号规格',@出库数量,'@生产批号','@备注')";
        //插入合同关联
        string strInsertWGCKHT = "insert into wgckht (出库单号,出库项次,生产计划号,生产计划项次,生产计划数量) values ('@出库单号',@出库项次,'@生产计划号','@生产计划项次',@生产计划数量)";
        //获取单据状态
        string strSelectWGCKMark = "select 单据状态 from wgcka where 出库单号='@出库单号'";
        //更新单据状态
        string strUpdateWGCKMark = "update wgcka set 单据状态=@单据状态 where 出库单号='@出库单号'";
        //更新审核时间
        string strUpdateWGCKAuditDate = "update wgcka set 审核时间=@审核时间 where 出库单号='@出库单号'";
        //获取指定结余号结余数量
        string strSelectWGJY = "select 数量 from wgjy where 结余号='@结余号'";
        //获取指定工单号工序结余数量
        string strSelectGXJY = "select gxjy008 工序结余 from gxjy_table where gxjy007='半成品库' and gxjy001='@工单号'";
        //更新指定结余号结余数量
        string strUpdateJY = "update wgjy set 数量=@结余数量 where 结余号='@结余号'";
        //获取指定用户编号的用户信息
        string strSelectUserInfo = "select yh002 用户名称,yh003 部门编号,yh004 部门名称 from yh_table where yh001='@用户编号'";
        //获取指定产品执行标准
        string strSelectProductInfo = "select ycl041 质量等级,ycl042 执行标准 from ycl_table where ycl001='@型号规格'";
        //插入工单
        string strInsertGD = "INSERT INTO gd_table(gd001, gd002, gd003, gd004, gd005, gd006, gd007, gd008, gd009, gd010, gd011, gd012, gd013, gd014, gd015, gd016, gd017, gd018, gd019, gd020, gd021, gd022, gd025, gd026, gd027, gd028, gd029, gd030, gd031, gd032, gd033, gd034, gd035, gd036, gd037) VALUES ('@工单号', '@工单日期', '@用户编号', '@用户名称', '@部门编号', '@部门名称', '@型号规格', '', '只', @数量, 0, 0, '', '', 1, '', '', '', '', 'Y', '@用户编号', getdate(), 'A', '', '', 1, '半成品库', '@执行标准', '@质量等级', '@用户编号', getdate(), '@生产批号', 0, '', '')";
        //插入工序结余
        string strInsertGXJY = "INSERT INTO gxjy_table( gxjy001, gxjy002, gxjy003, gxjy004, gxjy005, gxjy006, gxjy007, gxjy008, gxjy009, gxjy010, gxjy011, gxjy012, gxjy013, gxjy014, gxjy015, gxjy016, gxjy017, gxjy018) VALUES ('@工单号', '', '', '@型号规格', '', '只', '半成品库', '@数量', 0, 0, 'w', '', -1, -1, '@备注', '@生产批号', '', '')";
        //更新指定出库单项次工单号关联
        string strUpdateWGCKGD = "update wgckb set 工单号='@工单号' where 出库单号='@出库单号' and 出库项次=@出库项次";
        //删除指定工单
        string strDeleteGD = "delete from gd_table where gd001='@工单号'";
        //删除指定工序结余
        string strDeleteGXJY = "delete from gxjy_table where gxjy001='@工单号'";
#endregion

        public frmWGCKManage(string strTmpUserID)
        {
            InitializeComponent();
            strUserID = strTmpUserID;
        }

        #region 通用事件
        private void frmWGCKManage_Load(object sender, EventArgs e)
        {
            cmbDocStatus.SelectedIndex = 0;
            ButtonMode(0);
            sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
        }

        private void btnAddKeywords_Click(object sender, EventArgs e)
        {
            if (cmbSearchFields.Text.Length > 0 && txtSearchKeywords.Text.Trim().Length > 0)
            {
                if (lstKeywords.Items.Count > 0)
                {
                    for (int i = 0; i < lstKeywords.Items.Count; i++)
                    {
                        if (lstKeywords.Items[i].ToString().StartsWith(cmbSearchFields.Text))
                        {
                            lstKeywords.Items[i] = cmbSearchFields.Text + ":" + txtSearchKeywords.Text.Trim();
                        }
                    }
                }
                else
                {
                    lstKeywords.Items.Add(cmbSearchFields.Text + ":" + txtSearchKeywords.Text.Trim());
                }
            }
        }

        private void lstKeywords_DoubleClick(object sender, EventArgs e)
        {
            if (lstKeywords.SelectedItem != null)
            {
                lstKeywords.Items.Remove(lstKeywords.SelectedItem);
            }
        }

        private void ButtonMode(int iTmpMode)
        {
            switch (iTmpMode)
            {
                case 0:      //一般工作状态
                    gbSearch.Enabled = true;
                    btnAdd.Enabled = true;
                    btnEdit.Enabled = false ;
                    btnDelete.Enabled = false ;
                    btnConfirm.Enabled = false;
                    btnCancel.Enabled = false;
                    btnGetData.Enabled = false;
                    btnAudit.Enabled = false;
                    break;
                case 1:      //添加状态
                    gbSearch.Enabled = false;
                    btnAdd.Enabled = false ;
                    btnEdit.Enabled = false ;
                    btnDelete.Enabled = false ;
                    btnConfirm.Enabled = true ;
                    btnCancel.Enabled = true ;
                    btnGetData.Enabled = true ;
                    btnAudit.Enabled = false;
                    break;
                case 2:      //编辑状态
                    gbSearch.Enabled = false;
                    btnAdd.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnConfirm.Enabled = true;
                    btnCancel.Enabled = true;
                    btnGetData.Enabled = true;
                    btnAudit.Enabled = false;
                    break;
                case 3:      //查询状态
                    gbSearch.Enabled = true;
                    btnAdd.Enabled = true ;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnConfirm.Enabled = false ;
                    btnCancel.Enabled = false ;
                    btnGetData.Enabled = false ;
                    btnAudit.Enabled = false;
                    break;
                default :
                    clsTools.ShowWarningMsg(iTmpMode.ToString() + ":未设置的工作状态！");
                    break;
            }
        }

        private void FillRowIndex(DataGridView tmpdgv,string strTmpIDFieldName)
        {
            if (tmpdgv.Rows.Count > 0)
            {
                for (int i = 0; i < tmpdgv.Rows.Count; i++)
                {
                    tmpdgv.Rows[i].Cells[strTmpIDFieldName].Value = i+1;
                }
            }
        }
        #endregion 通用事件

        private string GetPPNo()
        {
            string strTmpMaxPPNo = clsTools.GetString(sqt.DoCmdScalarData(strGetMaxPPNo));
            string strTmpDate = string.Empty;
            string strTmpNo = string.Empty;
            if (strTmpMaxPPNo.Trim().Length == 0)
            {
                strTmpDate = clsTools.GetString(sqt.DoCmdScalarData(strGetServerDate)).Replace("-", "");
                strTmpNo = "0001";
            }
            else
            {
                strTmpDate = strTmpMaxPPNo.Substring(0, 8);
                int iTmpNo = clsTools.GetInt(strTmpMaxPPNo.Substring(8, 4)) + 1;
                strTmpNo = iTmpNo.ToString("0000");
            }
            return strTmpDate + strTmpNo;
        }

        private string GetGDNo()
        {
            string strTmpMaxGDNo = clsTools.GetString(sqt.DoCmdScalarData(strGetMaxGDNo));
            string strTmpDate = string.Empty;
            string strTmpNo = string.Empty;
            if (strTmpMaxGDNo.Trim().Length == 0)
            {
                strTmpDate = clsTools.GetString(sqt.DoCmdScalarData(strGetServerDate)).Replace("-", "");
                strTmpNo = "0001";
            }
            else
            {
                strTmpDate = strTmpMaxGDNo.Substring(0, 8);
                int iTmpNo = clsTools.GetInt(strTmpMaxGDNo.Substring(8, 4)) + 1;
                strTmpNo = iTmpNo.ToString("0000");
            }
            return strTmpDate + strTmpNo;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "ck3"))
            {
                return;
            }
            iWorkMode = 1;                                                                //设置工作模式为新增模式
            if (dgv1.DataSource != null)
            {
                DataTable tmpdt = (DataTable)dgv1.DataSource;
                tmpdt.Rows.Add(tmpdt.NewRow());
            }
            else
            {
                dgv1.Rows.Add();
            }
            dgv1.Rows[dgv1.Rows.Count - 1].Cells["dgv1_c1"].Value = GetPPNo();
            dgv1.CurrentCell = dgv1.Rows[dgv1.Rows.Count - 1].Cells[0];
            dgv1.Enabled = false;
            lblMark.Text = "";
            lblMark.Visible = false;
            dgv2.ContextMenuStrip = contextMenuStrip1;
            dgv2.Rows.Clear();
            dgv2.ReadOnly = false;
            ButtonMode(1);
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            frmKCSelector newFrmKCSelector = new frmKCSelector();
            newFrmKCSelector.Owner = this.Owner;
            newFrmKCSelector.ShowDialog();
            if (newFrmKCSelector.bCancel || newFrmKCSelector.dfrlst == null)
            {
                return;
            }
            if (newFrmKCSelector.dfrlst.Count > 0)
            {
                dgv2.Rows.Add(newFrmKCSelector.dfrlst.Count);
                for (int i = 0; i < newFrmKCSelector.dfrlst.Count; i++)
                {
                    dgv2.Rows[dgv2.Rows.Count - newFrmKCSelector.dfrlst.Count + i].Cells["dgv2_c2"].Value = newFrmKCSelector.dfrlst[i].strJYNo;
                    dgv2.Rows[dgv2.Rows.Count - newFrmKCSelector.dfrlst.Count + i].Cells["dgv2_c3"].Value = newFrmKCSelector.dfrlst[i].strModel;
                    dgv2.Rows[dgv2.Rows.Count - newFrmKCSelector.dfrlst.Count + i].Cells["dgv2_c4"].Value = newFrmKCSelector.dfrlst[i].iQuantity;
                    dgv2.Rows[dgv2.Rows.Count - newFrmKCSelector.dfrlst.Count + i].Cells["dgv2_c5"].Value = newFrmKCSelector.dfrlst[i].strSN;
                    dgv2.Rows[dgv2.Rows.Count - newFrmKCSelector.dfrlst.Count + i].Cells["dgv2_c6"].Value = newFrmKCSelector.dfrlst[i].strMemo;
                }
                dgv2.AutoResizeColumns();
                FillRowIndex(dgv2,"dgv2_c1");
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "ck2"))
            {
                return;
            }
            string strTmpSelectString = strSelectWGCKARange;
            for (int i = 0; i < lstKeywords.Items.Count; i++)
            {
                string strTmpFieldName = lstKeywords.Items[i].ToString().Split(':').GetValue(0).ToString();
                string strTmpKeyword = lstKeywords.Items[i].ToString().Split(':').GetValue(1).ToString();
                switch (strTmpFieldName)
                {
                    case "出库单号":
                        strTmpSelectString += " and b.出库单号 like '" + strTmpKeyword + "'";
                        break;
                    case "合同号":
                        strTmpSelectString += " and (select jhb024 from scjhb_table,wgckht where jhb001=生产计划号 and jhb002=生产计划项次 and wgckht.出库单号=b.出库单号) like '" + strTmpKeyword + "'";
                        break;
                    case "型号规格":
                        strTmpSelectString += " and 型号规格 like '" + strTmpKeyword + "'";
                        break;
                }
            }
            if (cmbDocStatus.SelectedIndex != 0)
            {
                strTmpSelectString += " and 单据状态=" + Convert.ToString(cmbDocStatus.SelectedIndex-1);
            }
            strTmpSelectString += ")";
            dgv1 .DataSource = sqt.GetDataTable(strTmpSelectString);
            if (dgv1.Rows.Count > 0)
            {
                dgv1.CurrentCell = dgv1.Rows[0].Cells[0];
            }
        }

        private void ReadData()
        {
            ButtonMode(3);
            if (dgv1.CurrentRow != null)
            {
                string strTmpCkNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value);
                DataTable tmpdt = sqt.GetDataTable(strSelectWGCKB.Replace("@出库单号", strTmpCkNo));
                switch (clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c3"].Value).Trim ())
                {
                    case "0":
                        lblMark.Text = "";
                        lblMark.Visible = false;
                        btnDelete.Enabled = true;
                        btnAudit.Enabled = true;
                        btnEdit.Enabled = true;
                        break;
                    case "1":
                        lblMark.Text = "核";
                        lblMark.Visible = true ;
                        btnDelete.Enabled = false;
                        btnAudit.Enabled = true;
                        btnEdit.Enabled = true;
                        break;
                }
                dgv2.Rows.Clear();
                if (tmpdt.Rows.Count > 0)
                {
                    dgv2.Rows.Add(tmpdt.Rows.Count);
                    for (int i = 0; i < tmpdt.Rows.Count; i++)
                    {
                        dgv2.Rows[i].Cells["dgv2_c1"].Value = tmpdt.Rows[i]["出库项次"];
                        dgv2.Rows[i].Cells["dgv2_c2"].Value = tmpdt.Rows[i]["结余号"];
                        dgv2.Rows[i].Cells["dgv2_c3"].Value = tmpdt.Rows[i]["型号规格"];
                        dgv2.Rows[i].Cells["dgv2_c4"].Value = tmpdt.Rows[i]["出库数量"];
                        dgv2.Rows[i].Cells["dgv2_c5"].Value = tmpdt.Rows[i]["生产批号"];
                        dgv2.Rows[i].Cells["dgv2_c6"].Value = tmpdt.Rows[i]["备注"];
                        string[] strTmpPP = new string[1];
                        string[] strTmpPX = new string[1];
                        int[] iTmpQuantity = new int[1];
                        DataTable tmpdtscjh = sqt.GetDataTable(strGetSCJHXC.Replace("@计划单号", strTmpCkNo)
                                                                                                                .Replace("@计划项次", tmpdt.Rows[i]["计划项次"].ToString()));
                        if (tmpdtscjh.Rows.Count > 0)
                        {
                            strTmpPP = new string[tmpdtscjh.Rows.Count];
                            strTmpPX = new string[tmpdtscjh.Rows.Count];
                            iTmpQuantity = new int[tmpdtscjh.Rows.Count];
                            for (int k = 0; k < tmpdtscjh.Rows.Count; k++)
                            {
                                strTmpPP[k] = tmpdtscjh.Rows[k]["生产计划号"].ToString().Trim();
                                strTmpPX[k] = tmpdtscjh.Rows[k]["生产计划项次"].ToString().Trim();
                                iTmpQuantity[k] = clsTools.GetInt(tmpdtscjh.Rows[k]["生产计划数量"].ToString());
                            }
                        }
                        dgv2.Rows[i].Cells["dgv2_c7"].Value=strTmpPP ;
                        dgv2.Rows[i].Cells["dgv2_c8"].Value=strTmpPX ;
                        dgv2.Rows[i].Cells["dgv2_c9"].Value = iTmpQuantity;
                    }
                    dgv2.AutoResizeColumns();
                }
            }
        }

        private void dgv1_CurrentCellChanged(object sender, EventArgs e)
        {
            ReadData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "ck4"))
            {
                return;
            }
            iWorkMode = 2;                                                                //设置工作模式为编辑模式
            dgv1.Enabled = false;
            dgv2.ContextMenuStrip = contextMenuStrip1;
            dgv2.Rows.Clear();
            dgv2.ReadOnly = false;
            ButtonMode(2);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "ck5"))
            {
                return;
            }
            if (dgv1.CurrentRow != null)
            {
                string strTmpCkNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value).Trim();
                DialogResult dr = MessageBox.Show("确定删除单号为" + strTmpCkNo + "的出库单？删除以后将无法恢复！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    sqt.DoCmdExecuteNonQuery(strDeleteWGCKA.Replace("@出库单号", strTmpCkNo));
                    sqt.DoCmdExecuteNonQuery(strDeleteWGCKB.Replace("@出库单号", strTmpCkNo));
                    sqt.DoCmdExecuteNonQuery(strDeleteWGCKHT.Replace("@出库单号", strTmpCkNo));
                    dgv1.Rows.Remove(dgv1.CurrentRow);
                }
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            switch (iWorkMode)
            {
                case 1:
                    if (dgv2.Rows.Count == 0)
                    {
                        clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                        return;
                    }
                    else
                    {
                        for (int i = dgv2.Rows.Count - 1; i >= 0; i--)
                        {
                            bool bBlankRow = true;
                            foreach (DataGridViewCell tmpdgvc in dgv2.Rows[i].Cells)
                            {
                                if (tmpdgvc.ColumnIndex == 0)
                                {
                                    continue;
                                }
                                if (clsTools.GetString(tmpdgvc.Value).Trim().Length > 0)
                                {
                                    bBlankRow = false;
                                    break;
                                }
                            }
                            if (!bBlankRow)
                            {
                                break;
                            }
                            else
                            {
                                dgv2.Rows.RemoveAt(i);
                            }
                        }
                        FillRowIndex(dgv2,"dgv2_c1");
                        if (dgv2.Rows.Count == 0)
                        {
                            clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                            return;
                        }
                        else
                        {
                            for (int i = 0; i < dgv2.Rows.Count; i++)
                            {
                                if (clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value).Trim().Length == 0 || clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value).Trim ().Length ==0|| clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value).Trim().Length == 0|| clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value).Trim().Length == 0)
                                {
                                    clsTools.ShowWarningMsg("结余号、型号规格、出库数量、生产批号不允许为空！");
                                    return;
                                }
                            }
                            string strTmpCkNo = GetPPNo();
                            dgv1.CurrentRow .Cells ["dgv1_c1"].Value = strTmpCkNo ;
                            if (sqt.DoCmdExecuteNonQuery(strInsertWGCKA.Replace("@出库单号", strTmpCkNo )) > 0)
                            {
                                for (int i = 0; i < dgv2.Rows.Count; i++)
                                {
                                    if (dgv2.Rows[i].Cells["dgv2_c7"].Value != null && dgv2.Rows[i].Cells["dgv2_c8"].Value != null && dgv2.Rows[i].Cells["dgv2_c9"].Value != null)
                                    {
                                        string[] strTmpPP = (string[])dgv2.Rows[i].Cells["dgv2_c7"].Value;
                                        string[] strTmpPX = (string[])dgv2.Rows[i].Cells["dgv2_c8"].Value;
                                        int[] iTmpQuantity = (int[])dgv2.Rows[i].Cells["dgv2_c9"].Value;
                                        for (int k = 0; k < strTmpPP.Length; k++)
                                        {
                                            if (strTmpPP[k].Trim().Length == 0)
                                            {
                                                continue;
                                            }
                                            sqt.DoCmdExecuteNonQuery(strInsertWGCKHT.Replace("@出库单号", strTmpCkNo )
                                                                                                                             .Replace("@出库项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                             .Replace("@生产计划号", strTmpPP[k].Trim())
                                                                                                                             .Replace("@生产计划项次", strTmpPX[k].Trim())
                                                                                                                             .Replace("@生产计划数量", iTmpQuantity[k].ToString()));
                                        }
                                    }
                                    sqt.DoCmdExecuteNonQuery(strInsertWGCKB.Replace("@出库单号", strTmpCkNo)
                                                                                                                         .Replace("@出库项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                         .Replace("@结余号", clsTools.GetString (dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                                        .Replace("@型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value))
                                                                                                                        .Replace("@出库数量", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value))
                                                                                                                        .Replace("@生产批号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value))
                                                                                                                        .Replace("@备注", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c6"].Value)));
                                }
                            }
                            ButtonMode(0);
                            btnConfirm.Enabled = false;
                            dgv2.ReadOnly = true;
                            dgv2.ContextMenuStrip = null;
                            dgv1.Enabled =true ;
                            iWorkMode = 0;
                        }
                    }
                    break;
                case 2:
                    if (dgv2.Rows.Count == 0)
                    {
                        clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                        return;
                    }
                    else
                    {
                        for (int i = dgv2.Rows.Count - 1; i >= 0; i--)
                        {
                            bool bBlankRow = true;
                            foreach (DataGridViewCell tmpdgvc in dgv2.Rows[i].Cells)
                            {
                                if (tmpdgvc.ColumnIndex == 0)
                                {
                                    continue;
                                }
                                if (clsTools.GetString(tmpdgvc.Value).Trim().Length > 0)
                                {
                                    bBlankRow = false;
                                    break;
                                }
                            }
                            if (!bBlankRow)
                            {
                                break;
                            }
                            else
                            {
                                dgv2.Rows.RemoveAt(i);
                            }
                        }
                        FillRowIndex(dgv2,"dgv2_c1");
                        if (dgv2.Rows.Count == 0)
                        {
                            clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                            return;
                        }
                        else
                        {
                            for (int i = 0; i < dgv2.Rows.Count; i++)
                            {
                                if (clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value).Trim().Length == 0 || clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value).Trim ().Length ==0|| clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value).Trim().Length == 0|| clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value).Trim().Length == 0)
                                {
                                    clsTools.ShowWarningMsg("结余号、型号规格、出库数量、生产批号不允许为空！");
                                    return;
                                }
                            }
                            string strTmpCkNo=clsTools .GetString ( dgv1.CurrentRow .Cells ["dgv1_c1"].Value ).Trim ();
                            sqt.DoCmdExecuteNonQuery(strDeleteWGCKB.Replace("@出库单号", strTmpCkNo ));
                            sqt.DoCmdExecuteNonQuery(strDeleteWGCKHT.Replace("@出库单号", strTmpCkNo));
                            for (int i = 0; i < dgv2.Rows.Count; i++)
                            {
                                if (dgv2.Rows[i].Cells["dgv2_c7"].Value != null && dgv2.Rows[i].Cells["dgv2_c8"].Value != null && dgv2.Rows[i].Cells["dgv2_c9"].Value != null)
                                {
                                    string[] strTmpPP = (string[])dgv2.Rows[i].Cells["dgv2_c7"].Value;
                                    string[] strTmpPX = (string[])dgv2.Rows[i].Cells["dgv2_c8"].Value;
                                    int[] iTmpQuantity = (int[])dgv2.Rows[i].Cells["dgv2_c9"].Value;
                                    for (int k = 0; k < strTmpPP.Length; k++)
                                    {
                                        if (strTmpPP[k].Trim().Length == 0)
                                        {
                                            continue;
                                        }
                                        sqt.DoCmdExecuteNonQuery(strInsertWGCKHT.Replace("@出库单号", strTmpCkNo )
                                                                                                                             .Replace("@出库项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                             .Replace("@生产计划号", strTmpPP[k].Trim())
                                                                                                                             .Replace("@生产计划项次", strTmpPX[k].Trim())
                                                                                                                             .Replace("@生产计划数量", iTmpQuantity[k].ToString()));
                                        }
                                    }
                                    sqt.DoCmdExecuteNonQuery(strInsertWGCKB.Replace("@出库单号", strTmpCkNo)
                                                                                                                         .Replace("@出库项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                         .Replace("@结余号", clsTools.GetString (dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                                        .Replace("@型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value))
                                                                                                                        .Replace("@出库数量", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value))
                                                                                                                        .Replace("@生产批号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value))
                                                                                                                        .Replace("@备注", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c6"].Value)));
                            }
                            ButtonMode(0);
                            btnConfirm.Enabled = false ;
                            dgv2.ReadOnly = true;
                            dgv2.ContextMenuStrip = null;
                            dgv1.Enabled =true ;
                            iWorkMode = 0;
                        }
                    }
                    break;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            switch (iWorkMode)
            {
                case 1:
                    iWorkMode = 0;
                    ButtonMode(0);
                    dgv1.Rows.Remove(dgv1.CurrentRow);
                    dgv2.Rows.Clear();
                    dgv1.Enabled = true;
                    dgv2.ReadOnly = true;
                    dgv2.ContextMenuStrip = null;
                    ReadData();
                    break;
                case 2:
                    iWorkMode = 0;
                    ButtonMode(0);
                    dgv1.Enabled = true;
                    dgv2.ReadOnly = true;
                    dgv2.ContextMenuStrip = null;
                    ReadData();
                    break;
            }
        }

        private void 添加行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dgv2.Rows.Add();
            dgv2.CurrentCell = dgv2.Rows[dgv2.Rows.Count - 1].Cells[0];
        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv2.CurrentRow != null)
            {
                dgv2.Rows.Remove(dgv2.CurrentRow);
            }
        }

        private void btnAudit_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "ck6"))
            {
                return;
            }
            if (dgv1.CurrentRow != null)
            {
                string strTmpCkNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value);
                int iMark = clsTools.GetInt(sqt.DoCmdScalarData(strSelectWGCKMark.Replace("@出库单号", strTmpCkNo)));
                if (iMark ==0)
                {
                    //检查结余是否够减
                    for (int i = 0; i < dgv2.Rows.Count; i++)
                    {
                        int iTmpQuantity = clsTools.GetInt(sqt.DoCmdScalarData(strSelectWGJY.Replace("@结余号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))));
                        if (iTmpQuantity < clsTools.GetInt(dgv2.Rows[i].Cells["dgv2_c4"].Value))
                        {
                            clsTools.ShowWarningMsg("第" + Convert.ToString(i + 1) + "项没有足够结余数量！");
                            return;
                        }
                    }
                    //减结余数量
                    for (int i = 0; i < dgv2.Rows.Count; i++)
                    {
                        sqt.DoCmdExecuteNonQuery(strUpdateJY.Replace("@结余号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                        .Replace("@结余数量", "数量-" + clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value)));
                    }
                    //生成新工单和新工单结余
                    DataTable tmpdtUserInfo = sqt.GetDataTable(strSelectUserInfo.Replace("@用户编号", strUserID));
                    string strUserName = string.Empty;
                    string strDepartmentID = string.Empty;
                    string strDepartmentName = string.Empty;
                    if (tmpdtUserInfo.Rows.Count > 0)
                    {
                        strUserName = clsTools.GetString(tmpdtUserInfo.Rows[0]["用户名称"]);
                        strDepartmentID = clsTools.GetString(tmpdtUserInfo.Rows[0]["部门编号"]);
                        strDepartmentName = clsTools.GetString(tmpdtUserInfo.Rows[0]["部门名称"]);
                    }
                    for (int i = 0; i < dgv2.Rows.Count; i++)
                    {
                        string strTmpGDNo = GetGDNo();
                        string strProductStandard = string.Empty;
                        string strProductClass = string.Empty;
                        DataTable tmpdtProductInfo = sqt.GetDataTable(strSelectProductInfo.Replace("@型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value)));
                        if (tmpdtProductInfo.Rows.Count > 0)
                        {
                            strProductStandard = clsTools.GetString(tmpdtProductInfo.Rows[0]["执行标准"]);
                            strProductClass = clsTools.GetString(tmpdtProductInfo.Rows[0]["质量等级"]);
                        }
                        sqt.DoCmdExecuteNonQuery(strInsertGD.Replace("@工单号", strTmpGDNo)
                                                                                        .Replace("@工单日期", strTmpGDNo.Substring(0, 8))
                                                                                        .Replace("@用户编号", strUserID)
                                                                                        .Replace("@用户名称", strUserName)
                                                                                        .Replace("@部门编号", strDepartmentID)
                                                                                        .Replace("@部门名称", strDepartmentName)
                                                                                        .Replace("@型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value))
                                                                                        .Replace("@数量", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value))
                                                                                        .Replace("@执行标准", strProductStandard)
                                                                                        .Replace("@质量等级", strProductClass)
                                                                                        .Replace("@生产批号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value)));
                        sqt.DoCmdExecuteNonQuery(strInsertGXJY.Replace("@工单号", strTmpGDNo)
                                                                                            .Replace("@型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value))
                                                                                            .Replace("@数量", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value))
                                                                                            .Replace("@备注", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c6"].Value))
                                                                                            .Replace("@生产批号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value)));
                        sqt.DoCmdExecuteNonQuery(strUpdateWGCKGD.Replace("@出库单号", strTmpCkNo)
                                                                                                    .Replace("@出库项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                    .Replace("@工单号", strTmpGDNo));
                        dgv2.Rows[i].Cells["dgv2_c10"].Value = strTmpGDNo;
                    }
                    sqt.DoCmdExecuteNonQuery(strUpdateWGCKMark.Replace("@出库单号", strTmpCkNo)
                                                                                                                    .Replace("@单据状态", "1"));
                    sqt.DoCmdExecuteNonQuery(strUpdateWGCKAuditDate.Replace("@出库单号", strTmpCkNo)
                                                                                                                    .Replace("@审核时间", "replace(CONVERT(varchar(12) , getdate(), 111),'/','-')"));
                    dgv1.CurrentRow.Cells["dgv1_c2"].Value = clsTools.GetString(sqt.DoCmdScalarData(strGetServerDate));
                    lblMark.Text = "核";
                    lblMark.Visible = true;
                }
                else
                {
                    if (iMark == 1)
                    {
                        //检查工序结余是否足够
                        for (int i = 0; i < dgv2.Rows.Count; i++)
                        {
                            int iTmpQuantity = clsTools.GetInt(sqt.DoCmdScalarData(strSelectGXJY .Replace("@工单号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c10"].Value))));
                            if (iTmpQuantity < clsTools.GetInt(dgv2.Rows[i].Cells["dgv2_c4"].Value))
                            {
                                clsTools.ShowWarningMsg("第" + Convert.ToString(i + 1) + "项没有足够结余数量！");
                                return;
                            }
                        }
                        //删除工单和工序结余
                        for (int i = 0; i < dgv2.Rows.Count; i++)
                        {
                            sqt.DoCmdExecuteNonQuery (strDeleteGD .Replace ("@工单号",clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c10"].Value)));
                            sqt.DoCmdExecuteNonQuery(strDeleteGXJY.Replace("@工单号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c10"].Value)));
                        }
                        //加结余数量
                        for (int i = 0; i < dgv2.Rows.Count; i++)
                        {
                            sqt.DoCmdExecuteNonQuery(strUpdateJY.Replace("@结余号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                            .Replace("@结余数量", "数量+" + clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value)));
                            dgv2.Rows[i].Cells["dgv2_c10"].Value = string.Empty ;
                        }
                        sqt.DoCmdExecuteNonQuery(strUpdateWGCKMark.Replace("@出库单号", strTmpCkNo)
                                                                                                                    .Replace("@单据状态", "0"));
                        sqt.DoCmdExecuteNonQuery(strUpdateWGCKAuditDate.Replace("@出库单号", strTmpCkNo)
                                                                                                                                    .Replace("@审核时间", "null"));
                        dgv1.CurrentRow.Cells["dgv1_c2"].Value = string.Empty;
                        lblMark.Text = "";
                        lblMark.Visible = false;
                    }
                }
            }
        }
    }
}